#include "BitMask.h"
#include ""

BitMask::BitMask(int w, int h, int s, int m)
{
	width = w;
	height = h;
	stamp_size = s;
	sample_rate = m;
	num_bit =width*height*sample_rate;

	// 1 bit is a not real 1 bit here
	buffer = new bit[num_bit];
	stamp_masks = 0;
}

void BitMask::clear()
{
	memset(buffer, 0, num_bit*sizeof(bit));
}

BitMask::~BitMask()
{
	if (buffer != 0)
		delete buffer;
}

void BitMask::setMaskBit(int screen_x, int screen_y, int n, bool b)
{
	bit[(screen_y*width+screen_x)*sample_rate+n] = b;
}

bit* BitMask::readMaskBits(int stamp_x, int stamp_y, int width, int height)
{
	//::must have the same pattern with bound samples, since it is a filter for bound samples. 
	int num = width*height*stamp_size*stamp_size*sample_rate;
	bit* masks =  new bit[num];
	for (int y = 0; y< height; y++)
		for (int x = 0; x < width; x++)
		{
			bit* b = stampBitMasks(stamp_x, stamp_y);
			int block_size = stamp_size*stamp_size*sample_rate;
			for (int k =0; k < block_size; k++)
			{
				masks[(y*width+x)*block_size + k] = b[k]; 
			}
		}

	return masks;
}

// x, y is stamp location 
bit* BitMask::stampBitMasks(int base_x, int base_y)
{
	if (stamp_masks != 0)
		delete stamp_masks;

	stamp_masks = new bit[stamp_size*stamp_size*sample_rate];
	for (int y = 0; y < stamp_size; y++)
		for (int x = 0; x < stamp_size; x++)
		{
			int screen_x =  base_x*stamp_size+x;
			int screen_y = base_y*stamp_size+y;
			for (int k = 0; k < sample_rate; k++)
				stamp_masks[(y*stamp_size+x)*sample_rate+k] = buffer[(screen_y*width+screen_x)*sample_rate];
		}

	return stamp_masks;
}